package sg.edu.nus.iss.club;


public class Club {
	private Member[] arrMember;
	
	private int memberNum = 0;
	private final int ARRAY_SIZE = 5;
	private int memberCnt;
	
	public Club(){
	}

	public Member getMember(int memberNum) {
		for(int i=0;i<arrMember.length;i++){
			if(arrMember[i] instanceof Member){
				if(arrMember[i].getMemberNumber() == memberNum){
					arrMember[i].show();
					return (Member) arrMember[i];
				}
			}
		}
		return null;
	}
	
	private void ensureArraySize(){
		if(arrMember == null ){
			arrMember = new Member[ARRAY_SIZE];
		}
		else if(memberNum >= arrMember.length ){
			Member[] mTemp = new Member[arrMember.length + ARRAY_SIZE];
			System.arraycopy(arrMember, 0, mTemp, 0, arrMember.length);
			arrMember = mTemp;
		}			
	}
	
	public Member addMember(String firstName, String surName, String middleName){
		ensureArraySize();
		memberNum++;
		Member mbr = new Member(surName,firstName,middleName,memberNum);
		arrMember[memberNum-1] = mbr;
		memberCnt++;
		
		return mbr;
	}
	
	public void removeMember(int memberNum){
		int count = 0;
		for(int i=0;i<arrMember.length;i++){
			if(arrMember[i] instanceof Member){
				if(arrMember[i].getMemberNumber()==memberNum){
					arrMember[i] = null;
					count++;
					break;
				}
			}
		}
		
		if(count > 0){
			memberCnt--;
		}
	}
	
	public Member[] getMembers(){
		return arrMember;	
	}
	
	public void showMembers(){
		if((arrMember == null) || (arrMember.length == 0)){
			System.out.println("There is no member.");
		}
		else{
			System.out.println(arrMember.length);
			for(int i = 0;i<arrMember.length;i++){			
				if(arrMember[i] instanceof Member){
					((Member) arrMember[i]).show();
				}
			}
			
			//System.out.println(memberCnt);
			//System.out.println(memberNum);
		}
		
	}
	
}
